class ResourcesController < ApplicationController
  # GET /resources
  # GET /resources.xml
  def index
	  country = session[:activeCountry]
    category = session[:activeCategory]
	  language = session[:activeLanguage]
		
		@resources = Resource.all.find_all do |a| 
   			found = (a.category_id == category) 
			  found &=  (a.country_id = country) 
				found &=  (a.language_id = language)
				found
		end
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @resources }
    end
  end

  # GET /resources/1
  # GET /resources/1.xml
  def show
    @resource = Resource.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @resource }
    end
  end

  # GET /selectedresources
  # GET /selectedresources.xml
  def selectedresources
	  category = Integer(params[:category])
	  country = Integer(params[:country])
	  language = Integer(params[:language])
		
		@resources = Resource.all.find_all do |a| 
   			found = (a.category_id == category) 
			  found &=  (a.country_id = country) 
				found &=  (a.language_id = language)
				found
		end

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :action => "selectedresources.rxml"}
    end
  end
		
	
	
  # GET /resources/new
  # GET /resources/new.xml
  def new
    @resource = Resource.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @resource }
    end
  end

  # GET /resources/1/edit
  def edit
    @resource = Resource.find(params[:id])
  end

  # POST /resources
  # POST /resources.xml
  def create
    @resource = Resource.new(params[:resource])
		@resource.category = Category.find(session[:selectedCategory])
		
    respond_to do |format|
      if @resource.save
        format.html { redirect_to(@resource, :notice => 'Resource was successfully created.') }
        format.xml  { render :xml => @resource, :status => :created, :location => @resource }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @resource.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /resources/1
  # PUT /resources/1.xml
  def update
    @resource = Resource.find(params[:id])

    respond_to do |format|
      if @resource.update_attributes(params[:resource])
        format.html { redirect_to(@resource, :notice => 'Resource was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @resource.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /resources/1
  # DELETE /resources/1.xml
  def destroy
    @resource = Resource.find(params[:id])
    @resource.destroy

    respond_to do |format|
      format.html { redirect_to(resources_url) }
      format.xml  { head :ok }
    end
  end
	
	#userprofile
	def userprofile
	 @countries = Country.all
	 @languages = Language.all
	  respond_to do |format|
      format.html #"userprofile.html.erb"
    end
	end
	
	def rootselection
	  session[:selectedcountry] = Integer(params[:selectedcountry])
	  session[:selectedlanguage] = Integer(params[:selectedlanguage])
	  @rootcategories = RootCategory.all
	  respond_to do |format|
      format.html #"rootselection.html.erb"
    end
	end 
	
	def categoryselection
	  session[:selectedroot] = Integer(params[:selectedroot])
		@categories = Category.all.find_all do |a| 
   			(a.root_category_id == session[:selectedroot]) 
		end
	end

	def resourceselection
	  session[:selectedCategory] = Integer(params[:selectedcategory])
	  country = session[:selectedcountry]
    category = session[:selectedCategory]
	  language = session[:selectedlanguage]
		
		
		@resources = Resource.all.find_all do |a| 
   			found = (a.category_id == category) 
			  found &=  (a.country_id = country) 
				found &=  (a.language_id = language)
				found
		end
	end

	def displayresource
		session[:selectedresource] = Integer(params[:selectedresource])
		@selectedresource = Resource.find(session[:selectedresource])
		@wikipage = "http://portlandwiki.org/" + @selectedresource.wiki_page + "?action=render"
	end
		
end
